CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
PROJECT(test)

MESSAGE(">>>>>> cmake begind================")

# 输出操作系统名称
EXECUTE_PROCESS(COMMAND uname -o)

# 列出当前文件目录内容项，通过"cmake"和"awk"过滤出文件名"cmake_install.cmake"
# 相当于执行命令: ls -al | grep "cmake" | awk "{print $9}"
EXECUTE_PROCESS(COMMAND ls -al
                COMMAND grep "cmake"
                COMMAND awk "{print $9}")

# 指定命令的工作目录，在指定目录下执行pwd命令
EXECUTE_PROCESS(COMMAND pwd
                WORKING_DIRECTORY "/home")


# 设置子命令的超时时间
# 注意，是针对每个子命令而言的超时时间，而不是命令总的执行时间
EXECUTE_PROCESS(COMMAND sleep 5
                COMMAND sleep 5
                TIMEOUT 4
                RESULT_VARIABLE msg)
MESSAGE("Commands execute result: ${msg}")

# 最后一个子命令是删除一个不存在的文件，即使前面的命令执行成功，它也只是包含最后一个子命令的执行结果
EXECUTE_PROCESS(COMMAND ls -al
                COMMAND rm "notexistfile.ext"
                RESULT_VARIABLE msg)
MESSAGE("Command execute result: ${msg}")


# 存储所有子命令的运行结果
EXECUTE_PROCESS(COMMAND ls -al
                COMMAND rm "notexistfile.ext"
                RESULTS_VARIABLE msg_all)
MESSAGE("ALL commands execute result: ${msg_all}")

EXECUTE_PROCESS(COMMAND echo "start"
                COMMAND rm "notexistfile.ext"
                COMMAND pwd
                COMMAND rm "notexistfile2.ext"
                COMMAND pwd
                WORKING_DIRECTORY "/home"
                RESULTS_VARIABLE msg_result
                OUTPUT_VARIABLE msg_out
                ERROR_VARIABLE msg_err)
MESSAGE("Commands execute results: ${msg_result}")
MESSAGE("Commands output: ${msg_out}")
MESSAGE("Commands error: ${msg_err}")


EXECUTE_PROCESS(COMMAND read
                COMMAND rm "noexistfile.ext"
                COMMAND rm "noexistfile2.ext"
                COMMAND echo "write message to output"
                INPUT_FILE input
                OUTPUT_FILE output
                ERROR_FILE error)

EXECUTE_PROCESS(
    COMMAND echo "execute commad"
    COMMAND ls "CMakeLists.txt"
    COMMAND_ECHO STDERR
    )

EXECUTE_PROCESS(
                COMMAND echo "hello world"
                OUTPUT_VARIABLE var_output
                ECHO_OUTPUT_VARIABLE # 如果没有指定这个选项，那么命令行终端不会有"hello world"的打印
                )
MESSAGE("output var: ${var_output}")

EXECUTE_PROCESS(COMMAND pwd
                COMMAND rm "noexistfile.ext"
                COMMAND echo "hello world"
                COMMAND_ERROR_IS_FATAL LAST)

EXECUTE_PROCESS(COMMAND pwd
                COMMAND rm "noexistfile.ext"
                COMMAND echo "hello world"
                COMMAND_ERROR_IS_FATAL ANY)